package com.example.fragmentdemo.fragment;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.fragmentdemo.R;
import com.example.fragmentdemo.adapter.FileAdapter;
import com.example.fragmentdemo.bean.FileBean;
import com.example.fragmentdemo.helper.SQLHelper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ExampleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ExampleFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public static boolean flag=false;

    public RecyclerView recyclerView;

    public SwipeRefreshLayout swipeRefreshLayout;

    private FileAdapter fileAdapter;
    private ArrayList<FileBean> dataList;
    public ExampleFragment() {
        // Required empty public constructor
    }



    public static ExampleFragment newInstance(ArrayList<FileBean> beanList) {
        ExampleFragment fragment = new ExampleFragment();
        Bundle args = new Bundle();
        args.putParcelableArrayList("data_list", beanList);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            dataList = getArguments().getParcelableArrayList("data_list");
            // 在这里处理 dataList 数据
            Log.d(TAG, "dataList: "+dataList);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_example, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.rec_list);
        swipeRefreshLayout=view.findViewById(R.id.swipe_refresh_layout);
        if (getArguments() != null) {
            handlerDownPullUpdate();

            dataList = getArguments().getParcelableArrayList("data_list");
            // 在这里处理 dataList 数据
            Log.d(TAG, "dataList: " + dataList);
            fileAdapter = new FileAdapter(dataList, requireContext());
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(fileAdapter);
        }else {
            flag=true;
        }




    }

    private void handlerDownPullUpdate(){
        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void run() {
                        fileAdapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }
        });
    }

}



